package MyDS.SingleLinkedList;

/**
 * @Author: ylc
 * @Description:无头单链表实现
 * @Date Created in 2022-03-28 16:03
 */
public class SingleLinkedList {
    private class Node {
        int val;
        Node next;
        public Node(int val){
            this.val = val;
        }
    }
    private int size;
    private Node head;

    /**
     * 头插法
     * @param val
     */
    public void addFirst(int val){
        Node node = new Node(val);
        if(head == null){
            head = node;
        }else{
            node.next = head;
            head = node;
        }
        size++;
    }

    /**
     * 尾插法
     * @param val
     */
    public void addLast(int val){
        addIndex(size,val);
    }

    /**
     * 任意位置插入（根据索引，第一个数据结点为0号下标）
     * @param index
     * @param val
     * @return
     */
    public boolean addIndex(int index, int val){
        if(rangeCheck(index)){
            if(index == 0) {
                addFirst(val);
                return true;
            }
            Node node = new Node(val);
            Node pre = head;
            //找到index的前驱结点
            for(int i = 0; i < index - 1; i++){
                pre = pre.next;
            }
            node.next = pre.next;
            pre.next = node;
            size++;
            return true;
        }else{
            System.err.println("add index illegal");
            return false;
        }
    }

    /**
     * 查找是否包含关键字key是否在单链表当中
     * @param key
     * @return
     */
    public boolean contains(int key){
        for(Node cur = head; cur != null; cur = cur.next){
            if(key == cur.val){
                return true;
            }
        }
        return false;
    }

    /**
     * 根据index查找对应值
     * @param index
     * @return
     */
    public int get(int index){
        if(rangeCheck(index)){
            Node node = head;
            for(int i = 0; i < index; i++){
                node = node.next;
            }
            return node.val;
        }else{
            System.err.println("get index illegal!");
            return -1;
        }
    }

    /**
     * 将单链表中索引为index的节点值改为newVal
     * @param index
     * @param newVal
     * @return  返回修改前的值
     */
    public int set(int index, int newVal){
        if(rangeCheck(index)){
            Node node = head;
            for(int i = 0; i < index; i++){
                node = node.next;
            }
            int oldVal = node.val;
            node.val = newVal;
            return oldVal;
        }else{
            System.err.println("set index illegal!");
            return -1;
        }
    }

    public void removeFirst(){
        removeIndex(0);
    }
    public void removeLast(){
        removeIndex(size - 1);
    }

    /**
     * 根据index删除对应值
     * @param index
     */
    public void removeIndex(int index){
        if(rangeCheck(index)){
            if(index == 0){
                Node temp = head;
                head = head.next;
                temp.next = null;
                size--;
            }else{
                Node pre = head;
                for(int i = 0; i < index - 1; i++){
                    pre = pre.next;
                }
                Node cur = pre.next;
                pre.next = cur.next;
                cur.next = null;
                size--;
            }
        }else{
            System.err.println("remove index illegal!");
        }
    }

    /**
     * 删除值为val的结点
     * @param val
     */
    public void removeValue(int val){
        if(head != null && head.val == val){
            Node temp = head;
            head = head.next;
            temp.next = null;
            size--;
        }else{
            Node pre = head;
            while(pre.next != null){
                if(pre.next.val == val){
                    Node cur = pre.next;
                    pre.next = cur.next;
                    cur.next = null;
                    size--;
                    return;
                }else{
                    pre = pre.next;
                }

            }
        }
    }

    /**
     * 删除所有值为val的结点
     * @param val
     */
    public void removeAllValue(int val){
        while(head != null && head.val == val){
            head = head.next;
            size--;
        }
        if(head == null){
            //此时链表中的值都为val
            return;
        }else{
            Node pre = head;
            while(pre.next != null){
                if(pre.next.val == val){
                    Node cur = pre.next;
                    pre.next = cur.next;
                    cur.next = null;
                    size--;
                }else{
                    pre = pre.next;
                }
            }
        }
    }

    /**
     * 返回链表的长度
     * @return
     */
    public int size(){
        return size;
    }

    /**
     * 检查下标范围合法性
     * @param index
     * @return
     */
    private boolean rangeCheck(int index){
        if(index < 0 || index >= size){
            return false;
        }
        return true;
    }


    public String toString() {
        String ret = "";
        Node node = head;
        while (node != null) {
            ret += node.val;
            ret += "->";
            node = node.next;
        }
        ret += "NULL";
        return ret;
    }
}
